package com.mune.gulimall.member.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfig {

  @Bean("requestInterceptor")
  public RequestInterceptor requestInterceptor() {
    return (requestTemplate) -> {
      ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      if (attributes != null) {
        HttpServletRequest request = attributes.getRequest();
        // 同步请求数据cookie
        String cookie = request.getHeader("Cookie");
        // 给远程调用请求中添加Cookie信息
        requestTemplate.header("Cookie", cookie);
      }
    };
  }
}
